package com.qunar.test;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.junit.Before;
import org.junit.Test;

import com.qunar.crawler.bean.Account;
import com.qunar.crawler.bean.Go117;
import com.qunar.crawler.bean.ProxySetting;
import com.qunar.crawler.html.HtmlAction;
import com.qunar.crawler.html.HtmlPossesser117Go;
import com.qunar.crawler.utils.ConfigBatchInit;
import com.qunar.crawler.utils.HumanActionSimulator4VisitingWeb;

/**
 * 
 */

/**
 * @author tao.zhang
 */
public class MyTeest {

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testAccountList() {
        List<Account> accounts = new ArrayList<Account>();
        accounts = ConfigBatchInit.getAccountList();

        for (Account a : accounts) {
            System.out.println(a.toString());
        }
    }

    @Test
    public void testProxyList() {
        List<ProxySetting> proxys = new ArrayList<ProxySetting>();
        proxys = ConfigBatchInit.getProxyList();

        for (ProxySetting a : proxys) {
            System.out.println(a.toString());
        }
    }

    @Test
    public void testAgentList() {
        List<String> agents = new ArrayList<String>();
        agents = ConfigBatchInit.getStringList("userAgentList");
        for (String a : agents) {
            System.out.println(a.toString());
        }
    }

    @Test
    public void testRandomProxy() {

        for (int i = 0; i < 10; i++) {

            System.out.println(ConfigBatchInit.getRandomProxySetting().toString());
        }
    }

    @Test
    public void testRandomAgent() {

        for (int i = 0; i < 10; i++) {

            System.out.println(ConfigBatchInit.getRandomAgent().toString());
        }
    }

    @Test
    public void testHtmlDownloaderGet() {

    	HtmlAction http = new HtmlAction();
    	
        System.out.println(http.get("http://lvtu.qunar.com"));
    }

    @Test
    public void testGetTourUrls() {

        Queue<String> q = new LinkedList<String>();
        HtmlPossesser117Go.fillUrlQueue(q);
        for (String url : q) {
            System.out.println(url);
        }
        System.out.println("队列中共" + q.size() + "条URL");
    }

//    @Test
//    public void testExecute() {
//
//        String tourUrlBase = "http://117go.com/all_tour_list_p";
//        String tourUrlEnd = ".html";
//        List<String> tourUrls = new ArrayList<String>();
//        long start = System.currentTimeMillis();
//        for (int i = 1; i < 10; i++) {
//            String tourlistPage = new String();
//            tourlistPage = HtmlAction.get(tourUrlBase + i + tourUrlEnd);
//            HtmlPossesser117Go.digTourUrls(tourlistPage, tourUrls);
//        }
//
//        long spend = System.currentTimeMillis() - start;
//        for (String url : tourUrls) {
//            System.out.println(url);
//        }
//        System.out.println("本次共爬到" + tourUrls.size() + "个旅程;耗时：" + spend / 1000 + "s");
//
//        /** ----url内存去重---- */
//        if (CollectionUtils.isNotEmpty(tourUrls)) {
//            Set set = new HashSet(tourUrls);
//            tourUrls.clear();
//            tourUrls.addAll(set);
//        }
//
//        System.out.println("去重后，旅程个数为：" + tourUrls.size());
//
//        DBUtil.addTours2DB(tourUrls);
//
//    }

    @Test
    public void testDig117Go() {

        String url = "http://117go.com/tour72316.html";
        HtmlAction http = new HtmlAction();
        String page = http.get(url);
        System.out.println(page);
//        List<Go117> go117s = new LinkedList<Go117>();
//        HtmlPossesser117Go.digGo117(page, go117s);
//
//        for (Go117 g : go117s)
//            System.out.println(g);

    }

//    @Test
//    public void testTourCrawler() {
//        TourCrawler t = new TourCrawler();
//        t.start();
//    }

    @Test
    public void testHumanAction() throws ParseException {
        if(HumanActionSimulator4VisitingWeb.getInstance().inInterval())
            System.out.println("现在正处于可抓取时间");
    }

}
